$(document).ready(function(){
	
	if(typeof(Edushare) == "undefined") Edushare = {};
	if(typeof(Edushare.assnCreate) == "undefined") Edushare.assnCreate = 
															{
																	isDiscarded:false
															}; 
//	var Edushare.assnCreate.isDiscarded = false;
	
	$('#asnStartDate')
		.datepicker()
		.datepicker('option', {showAnim : 'fadeIn'})
		.datepicker('option', 'dateFormat', 'd M yy');
	
	if($('#hAsnStartDate').val()!='undefined' && $('#hAsnStartDate').val()!=''){
		$('#asnStartDate').datepicker('setDate', $.datepicker.parseDate('d M yy', $('#hAsnStartDate').val()));
	}
	else{
		$('#asnStartDate').datepicker('setDate',new Date());
	}
	
	$('#asnStartDate').change(function() {
			var d1 = $.datepicker.parseDate('d M yy', $(this).val());
			var d2 = $.datepicker.parseDate('d M yy', $("#asnEndDate").val());
			if(d1 > d2)
				$("#asnEndDate").val($(this).val());
			
			if($("#asnLateCheck").is(':checked')){
				var d3 = $.datepicker.parseDate('d M yy', $("#asnLateDate").val());
				if(d1 > d3){ 
					$("#asnLateDate").val($(this).val());
				}
			}
		});
	
	
	//Set Format
	$('#asnEndDate')
	.datepicker()
	.datepicker('option', {showAnim : 'fadeIn'})
	.datepicker('option', 'dateFormat', 'd M yy');
	
	//If date is already set.. display that date else today.
	if($('#hAsnEndDate').val()!='undefined' && $('#hAsnEndDate').val()!=''){
		$('#asnEndDate').datepicker('setDate', $.datepicker.parseDate('d M yy', $('#hAsnEndDate').val()));
	}
	else{
		$('#asnEndDate').datepicker('setDate',new Date());
	}
	
	//if this date changes, change the other one as well
	$('#asnEndDate').change(function() {
		var d1 = $.datepicker.parseDate('d M yy', $(this).val());
		var d2 = $.datepicker.parseDate('d M yy', $("#asnStartDate").val());
		
		if(d2 > d1)
			$("#asnStartDate").val($(this).val());
		
		if($("#asnLateCheck").is(':checked')){
			var d3 = $.datepicker.parseDate('d M yy', $("#asnLateDate").val());
			if(d1 > d3 ){
				$("#asnLateDate").val($(this).val());
			}
		}
	});
	
	
	/*------- Late Submission --------*/
	if($("#asnLateCheck").is(':checked')){
		$('#asnLateDate').removeAttr('disabled');
		$('#asnLateDate')
			.datepicker()
			.datepicker('option', {showAnim : 'fadeIn'})
			.datepicker('option', 'dateFormat', 'd M yy')
			.datepicker('setDate', $.datepicker.parseDate('d M yy', $("#hAsnLateDate").val()));
	}
		
	$("#asnLateCheck").click(function(){
		if($(this).is(':checked')){
			$('#asnLateDate').removeAttr('disabled');
			$('#asnLateDate')
				.datepicker()
				.datepicker('option', {showAnim : 'fadeIn'})
				.datepicker('option', 'dateFormat', 'd M yy')
				.datepicker('setDate', $.datepicker.parseDate('d M yy', $("#asnEndDate").val()));
			
			
		}
		else{
			$('#asnLateDate').val('');
			$('#asnLateDate').attr('disabled','disabled');
		}
	});
	
	
	$('#asnDiscardButton').click(function(){
		Edushare.assnCreate.isDiscarded = true;
	});
	
	/*----- Form Submit ------*/
	$('#asnCreateForm').submit(function(){
		
		//if discarded return without verification
		if(Edushare.assnCreate.isDiscarded){
			//rest and return
			Edushare.assnCreate.isDiscarded = false;
			return;
		}

		//set value to hidden fields from date fields
		$('#hAsnStartDate').val($('#asnStartDate').val());
		$('#hAsnEndDate').val($('#asnEndDate').val());
		if($("#asnLateCheck").is(':checked')){
			$('#hAsnLateDate').val($('#asnLateDate').val());
		}
		
		//if title is valid
		if(jQuery.trim($('#asnTitle').val()) == ''){
			Edushare.FunctionalTextFields.SetError('Assignemnt Title is required');
			return false;
		}
		
		//if dates are correct
		var d0 = new Date();
		var d1 = $.datepicker.parseDate('d M yy', $("#asnStartDate").val());
		var d2 = $.datepicker.parseDate('d M yy', $("#asnEndDate").val());
		//set d0 on d0 morning
		d0.setHours(0, 0, 0, 0);
		
		if(d2 < d1){
			Edushare.FunctionalTextFields.SetError('End date should be after start date.');
			return false;
		}
		
		if(d2 < d0){
			Edushare.FunctionalTextFields.SetError('End date should be today or future.');
			return false;
		}		
		
		
		if($("#asnLateCheck").is(':checked')){
			var d3 = $.datepicker.parseDate('d M yy', $("#asnLateDate").val());
			if(d3 < d2){
				Edushare.FunctionalTextFields.SetError('Late submission date should be on or after end date.');
				return false;
			}
		}
		
		return true;
	});
	
});